package com.wuax.controller;

import com.wuax.domain.Borrow;
import com.wuax.domain.vo.Result;
import com.wuax.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/borrow")
public class BorrowController {

    @Autowired
    private BorrowService borrowService;

    /**
     * 借出图书
     */
    @PostMapping("/borrow")
    @ResponseBody
    public Result borrow(@RequestBody Borrow borrow) {
        try {
            return borrowService.borrow(borrow);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 归还图书
     */
    @PostMapping("/return")
    @ResponseBody
    public Result returnBook(@RequestParam List<String> borrowIds) {
        try {
            return borrowService.returnBook(borrowIds);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
